import Axios, {
  AxiosInstance,
  AxiosRequestConfig,
  CustomParamsSerializer
} from "axios";
import {
  PureHttpError,
  RequestMethods,
  PureHttpResponse,
  PureHttpRequestConfig
} from "./types.d";
import { stringify } from "qs";
import NProgress from "../progress";
import {
  getToken,
  formatToken,
  ExpiresInKey,
  RefreshTokenKey,
  removeToken,
  removeUserInfo
} from "@/utils/auth";
import { useUserStoreHook } from "@/store/modules/user";
import { storageLocal } from "@pureadmin/utils";
import { message } from "@/utils/message";
import router from "@/router";
const { VITE_TENANT_ID, VITE_CLIENT_ID } = import.meta.env;

// 相关配置请参考：www.axios-js.com/zh-cn/docs/#axios-request-config-1
const defaultConfig: AxiosRequestConfig = {
  // 请求超时时间
  timeout: 10000,
  headers: {
    Accept: "application/json, text/plain, */*",
    "Content-Type": "application/json",
    "X-Requested-With": "XMLHttpRequest",
    axiosclient: "uuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuuu"
  },
  // 数组格式参数序列化（https://github.com/axios/axios/issues/5142）
  paramsSerializer: {
    serialize: stringify as unknown as CustomParamsSerializer
  }
};

class PureHttp {
  constructor() {
    this.httpInterceptorsRequest();
    this.httpInterceptorsResponse();
  }

  /** token过期后，暂存待执行的请求 */
  private static requests = [];

  /** 防止重复刷新token */
  private static isRefreshing = false;

  /** 初始化配置对象 */
  private static initConfig: PureHttpRequestConfig = {};

  /** 保存当前Axios实例对象 */
  private static axiosInstance: AxiosInstance = Axios.create(defaultConfig);

  /** 重连原始请求 */
  private static retryOriginalRequest(config: PureHttpRequestConfig) {
    return new Promise(resolve => {
      PureHttp.requests.push((token: string) => {
        config.headers["Authorization"] = formatToken(token);
        resolve(config);
      });
    });
  }

  /**
   * refreshtoken处理
   */
  private static refreshTokenRequest() {
    if (!PureHttp.isRefreshing) {
      const refreshToken = storageLocal().getItem(RefreshTokenKey);
      if (refreshToken) {
        PureHttp.isRefreshing = true;
        // token过期刷新
        useUserStoreHook()
          .handRefreshToken({
            clientId: VITE_CLIENT_ID,
            refreshToken: refreshToken
          })
          .then(res => {
            const token = res.accessToken;
            PureHttp.requests.forEach(cb => cb(token));
            PureHttp.requests = [];
          })
          .catch(() => {
            removeToken();
            removeUserInfo();
            router.push("/login");
          })
          .finally(() => {
            PureHttp.isRefreshing = false;
          });
      } else {
        removeToken();
        removeUserInfo();
        router.push("/login");
      }
    }
  }

  /** 请求拦截 */
  private httpInterceptorsRequest(): void {
    PureHttp.axiosInstance.interceptors.request.use(
      async (config: PureHttpRequestConfig): Promise<any> => {
        console.log(
          "httpInterceptorsRequesthttpInterceptorsRequesthttpInterceptorsRequesthttpInterceptorsRequest"
        );
        // 开启进度条动画
        NProgress.start();
        // 优先判断post/get等方法是否传入回调，否则执行初始化设置等回调
        if (typeof config.beforeRequestCallback === "function") {
          config.beforeRequestCallback(config);
          return config;
        }
        if (PureHttp.initConfig.beforeRequestCallback) {
          PureHttp.initConfig.beforeRequestCallback(config);
          return config;
        }
        if (config.url.indexOf("__tenant") < 0) {
          config.url +=
            (config.url.indexOf("?") > -1 ? "&" : "?") +
            "__tenant=" +
            VITE_TENANT_ID;
        }
        /** 请求白名单，放置一些不需要token的接口（通过设置请求白名单，防止token过期后再请求造成的死循环问题） */
        const whiteList = [
          "/refresh-token",
          "/request-token-by-verification-code",
          "/send-verification-code"
        ];
        return whiteList.some(v => config.url.indexOf(v) > -1)
          ? config
          : new Promise(resolve => {
              const token = getToken();
              if (token) {
                const now = new Date().getTime();
                const expired =
                  parseInt(storageLocal().getItem(ExpiresInKey)) - now <= 0;
                if (expired) {
                  PureHttp.refreshTokenRequest();
                  resolve(PureHttp.retryOriginalRequest(config));
                } else {
                  config.headers["Authorization"] = formatToken(token);
                  resolve(config);
                }
              } else {
                resolve(config);
              }
            });
      },
      error => {
        return Promise.reject(error);
      }
    );
  }

  /** 响应拦截 */
  private httpInterceptorsResponse(): void {
    const instance = PureHttp.axiosInstance;
    instance.interceptors.response.use(
      (response: PureHttpResponse) => {
        console.log(
          "httpInterceptorsResponsehttpInterceptorsResponsehttpInterceptorsResponsehttpInterceptorsResponse"
        );
        const $config = response.config;
        // 关闭进度条动画
        NProgress.done();
        // 优先判断post/get等方法是否传入回调，否则执行初始化设置等回调
        if (typeof $config.beforeResponseCallback === "function") {
          $config.beforeResponseCallback(response);
          return response.data;
        }
        if (PureHttp.initConfig.beforeResponseCallback) {
          PureHttp.initConfig.beforeResponseCallback(response);
          return response.data;
        }
        return response.data;
      },
      (error: PureHttpError) => {
        if (error.response && error.response.status === 401) {
          PureHttp.refreshTokenRequest();
          return new Promise(resolve => {
            PureHttp.retryOriginalRequest(error.config).then(
              (config: PureHttpRequestConfig) => {
                console.log(config);
                PureHttp.axiosInstance
                  .request(config)
                  .then((response: PureHttpResponse) => {
                    resolve(response.data);
                  });
              }
            );
          });
        } else {
          message(error.response.data["error"]["message"], { type: "error" });
        }
        const $error = error;
        $error.isCancelRequest = Axios.isCancel($error);
        // 关闭进度条动画
        NProgress.done();
        // 所有的响应异常 区分来源为取消请求/非取消请求
        return Promise.reject($error.response.data);
      }
    );
  }

  /** 通用请求工具函数 */
  public request<T>(
    method: RequestMethods,
    url: string,
    param?: AxiosRequestConfig,
    axiosConfig?: PureHttpRequestConfig
  ): Promise<T> {
    const config = {
      method,
      url,
      ...param,
      ...axiosConfig
    } as PureHttpRequestConfig;

    // 单独处理自定义请求/响应回调
    return new Promise((resolve, reject) => {
      PureHttp.axiosInstance
        .request(config)
        .then((response: undefined) => {
          resolve(response);
        })
        .catch(error => {
          reject(error);
        });
    });
  }

  /** 单独抽离的post工具函数 */
  public post<T, P>(
    url: string,
    params?: AxiosRequestConfig<T>,
    config?: PureHttpRequestConfig
  ): Promise<P> {
    return this.request<P>("post", url, params, config);
  }

  /** 单独抽离的get工具函数 */
  public get<T, P>(
    url: string,
    params?: AxiosRequestConfig<T>,
    config?: PureHttpRequestConfig
  ): Promise<P> {
    return this.request<P>("get", url, params, config);
  }

  public getAxios() {
    return PureHttp.axiosInstance;
  }
}

export const http = new PureHttp();

export default http.getAxios();
